import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import moment from 'moment'
import http from './utils/http'
import BaseTable from './components/BaseTable.vue'
import ResTree from './components/ResTree.vue'
import AdminGroupTree from './components/AdminGroupTree.vue'
import ElTreeSelect from './components/ElTreeSelect.vue'
import BaseSelect from './components/BaseSelect.vue'
import ElTreeTable from './components/ElTreeTable.vue'

Vue.config.productionTip = false
Vue.prototype.$http = http;
Vue.component('base-table', BaseTable)
Vue.component('res-tree', ResTree)
Vue.component('admin-group-tree', AdminGroupTree)
Vue.component('el-tree-select', ElTreeSelect)
Vue.component('base-select', BaseSelect)
Vue.component('el-tree-table', ElTreeTable)

Vue.prototype.$moment = moment;

/**权限指令**/
Vue.directive('auth', {
    inserted: function(el, binding, vnode) {
        if (!Vue.prototype.$_auth(binding.value)) {
            el.parentNode.removeChild(el);
        }
    }
});
//权限检查方法
Vue.prototype.$_auth = function(value) {
    let isExist = false;
    let resList = store.state.resTreeList;
    let curMenu = store.state.activeTab;
    if (resList == undefined || resList == null || resList.length == 0) {
        return false;
    }
    for (let i = 0; i < resList.length; i++) {
        if (isExist) {
            break;
        }
        let parentMenu = resList[i];
        if (parentMenu.children && parentMenu.children.length > 0) {
            let realMenus = parentMenu.children;
            let realMenu = realMenus.find(item => {
                return item.code == curMenu;
            })
            if (realMenu && realMenu.children && realMenu.children.length > 0) {
                let findBtn = realMenu.children.find(btn => {
                    return btn.code == value
                })
                if (findBtn) {
                    isExist = true;
                }
            }
        }
    }
    return isExist;
};


new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')